class TrafficViolation < ActiveRecord::Base
  
  paginates_per 10

  def self.search( options={}, user )
    
    key = 'd5cf691cd875b7d817e7b9956a8b4276'
    url_query = 'http://v.juhe.cn/wz/query'

    response = RestClient.get url_query, {:params => { "key" => key, "city" => 'XJ_WULUMUQI', 'hphm' => options[:plate_number], 'classno' => options[:frame_number] } }

    logger.info "response.body:    "+response.body
    json = JSON.parse response.body
    
    tvs = []
    _user_id = ( user.nil? ? nil : user.id )
    json['result']['lists'].each_with_index do |j, index|
      tv = TrafficViolation.create(
              plate_number: options[:plate_number],
              frame_number: options[:frame_number],
              voilated_at: j["date"],
              location: j["area"],
              action: j["act"],
              code: j["code"],
              deduction: j["fen"],
              fine_amount: j["money"],
              handled: j["handled"],
              user_id: _user_id
              )

      tvs << tv
    end
    tvs
  end

end
